package com.mapper;

import com.pojo.Notifications;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface NotificationsMapper {

    @Insert("INSERT INTO notifications(user_id, type, source_id, target_id, `read`, created_at) " +
            "VALUES(#{userId}, #{type}, #{sourceId}, #{targetId}, #{read}, #{createdAt})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insert(Notifications notification);

    @Update("UPDATE notifications SET " +
            "user_id = #{userId}, " +
            "type = #{type}, " +
            "source_id = #{sourceId}, " +
            "target_id = #{targetId}, " +
            "`read` = #{read} " +
            "WHERE id = #{id}")
    int update(Notifications notification);

    @Delete("DELETE FROM notifications WHERE id = #{id}")
    int delete(Integer id);

    @Select("SELECT * FROM notifications WHERE id = #{id}")
    Notifications selectById(Integer id);

    @Select("SELECT * FROM notifications WHERE user_id = #{userId} ORDER BY created_at DESC")
    List<Notifications> selectByUserId(Integer userId);

    @Select("SELECT * FROM notifications WHERE user_id = #{userId} AND `read` = #{read} ORDER BY created_at DESC")
    List<Notifications> selectByUserIdAndReadStatus(@Param("userId") Integer userId, @Param("read") Boolean read);

    @Update("UPDATE notifications SET `read` = true WHERE id = #{id}")
    int markAsRead(Integer id);

    @Update("UPDATE notifications SET `read` = true WHERE user_id = #{userId}")
    int markAllAsRead(Integer userId);
}